<?php

/**
 * @Author:      zwh
 * @DateTime:    2016-10-24 14:01:13
 * @Description: 镜像单操作
 */
class ScsMirrorOrderController extends BaseController
{
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-24 14:04:15
     * @Description: 镜像单列表
     */
    public function lists(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "order_id"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)||$orders->order_type!=1){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if(!in_array($orders->status,Orders::$complete)){
                $response = Lang::get("scs_error.order_status_error_40026");
                break;
            }
            $mo=MirrorOrders::where('relation_order',Input::get('order_id'))->orderBy('updated_at','desc')->select('order_id','updated_at')->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("mo"=>$mo,'pay_time'=>$orders->pay_time,'order_id'=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "lists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-24 14:57:07
     * @Description: 创建镜像单
     */
    public function create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders= Orders::find(Input::get('order_id'));
            if(is_null($orders)||$orders->order_type!=1){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            $response=MirrorOrders::saveMirrorOrders(Input::get('order_id'),$orders);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 09:43:07
     * @Description: 订单详情
     */
    public function show(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $mirror_order=MirrorOrders::find(Input::get('order_id'));
            if(is_null($mirror_order)){//服务单才判断
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($mirror_order->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
    
            }
            
            $origin_order = Orders::where("order_id",$mirror_order->relation_order)->first();
            
            $engineer=MaintenanceEngineers::find($mirror_order->engineer_id);
            $engineer_name=isset($engineer->name) ? $engineer->name: "";
            $mirror_order->engineer_name=$engineer_name;
            //镜像单产品信息
            $mirror_op=MirrorOrderProduct::getOrderProductById($mirror_order->order_id);
            $remind=$mirror_op['remind'];            
            unset($mirror_op['remind']);
            $temp_arr=array("order_info"=>$mirror_order,"product_info"=>$mirror_op,"remind"=>$remind,"origin_order"=>$origin_order);
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $temp_arr);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "order_show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 14:32:07
     * @Description: 
     */
    public function update_order_car_info(){
    	$validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
    	do{
    		if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $uci=UserCarInfo::leftJoin("users","users.id","=","user_car_info.user_id")
            ->leftJoin("anhui_data.car_standard_info","anhui_data.car_standard_info.id","=","user_car_info.car_model_id")
            ->where('user_car_info.id',Input::get('id'))
            ->first();
            if($uci->type==2){//如果是企业用户
                $compnay=Companys::find($uci->user_id);
                $uci->company_name=isset($compnay)?$compnay->name:"";
            }else{
                $uci->company_name="";
            }
            $csi=DataCarStandardInfo::find($uci->car_model_id);
            $mirror_orders=MirrorOrders::find(Input::get('order_id'));
            $mirror_orders->user_id=$uci->user_id;
            $mirror_orders->user_name=$uci->real_name;
            $mirror_orders->user_sex=$uci->sex;
            $mirror_orders->car_no=$uci->car_no;
            $mirror_orders->user_type=$uci->type;
            $mirror_orders->company_name=$uci->company_name;
            $mirror_orders->car_brand_id = $csi->brand_name_id;
            $mirror_orders->car_brand_name = $csi->brand_name;
            $mirror_orders->car_series_id = $csi->series_name_id;
            $mirror_orders->car_series_name = $csi->series_name;
            $mirror_orders->car_model_id = $csi->id;
            $mirror_orders->car_model_name = $csi->model_name;
            $mirror_orders->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
    	}while(false);
    	Log::info("info", array("system" => "scs" ,"event" => "update_order_car_info", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-26 16:22:39
     * @Description: 修改虚拟订单结算时间
     */
    public function update_order_pay_time(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'pay_time' => 'required',
                'order_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $pay_time = Input::get('pay_time');
            
            $mirror_orders=MirrorOrders::find(Input::get('order_id'));
            if ($pay_time < $mirror_orders->meet_time) {
                $response = Lang::get("scs_error.mirror_orders_update_error_42008");
                break;
            }
            
            $mirror_orders->pay_time=$pay_time;
            $mirror_orders->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "update_order_pay_time", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zy
     * @DateTime:    2018-05-21
     * @Description: 修改虚拟订单接车时间
     */
    public function update_order_meet_time(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'meet_time' => 'required',
                'order_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $meet_time = Input::get('meet_time');
            
            $mirror_orders=MirrorOrders::find(Input::get('order_id'));
            if ($meet_time > $mirror_orders->pay_time) {
                $response = Lang::get("scs_error.mirror_orders_update_error_42009");
                break;
            }
            
            $mirror_orders->meet_time=$meet_time;
            $mirror_orders->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "update_order_meet_time", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-01-06 13:43:32
     * @Description: 修改订单中用户信息
     */
    public function update_order_user_info(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'name' => 'required',
                'phone' => 'required',
                'sex' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)||$orders->order_type!=1){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
    
            }
            $orders->phone=Input::get('phone');
            $orders->name=Input::get('name');
            $orders->sex=Input::get('sex');
            $orders->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "update_order_user_info", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 15:07:19
     * @Description: 用于添加 镜像单订单标准包
     */
    public function addProduct(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id'=>'required',
                "id"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mirror_orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($mirror_orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($mirror_orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $csi=DataCarStandardInfo::find($mirror_orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.car_model_is_empty23000");
                break;
            }
            $package=ProductPackage::find(Input::get("id"));
            if(is_null($package)){
                $response = Lang::get("scs_error.package_error_50001");
                break;
            }
            if($package->status!=1){
                $response = Lang::get("scs_error.package_status_error_50002");
                break;
            }
            // $ppc=ProductPackageCity::where("city_id",$mirror_orders->city)->where("status",1)->where("pack_id",$package->id)->first();
            $ppc=ProductPackageShop::isShopPackage($mirror_orders->shop_id,$package->id);
            if(!$ppc){
                $response = Lang::get("scs_error.package_unable_error_50003");
                break;
            }
            $p_item=ProductPackage::getItem($mirror_orders->car_model_id,$package->id);
            $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            if(is_null($whp)){
                $response = Lang::get("scs_error.work_hour_error_10001");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $count_amount=0;
            $supply_arr=$p_item['supply'];
            $order_product_item=array();
            foreach ($supply_arr as $_sa) {
                //更换耗材的加价系数
                $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$_sa["brand_id"])->where("supply_subject_id",$_sa["subject"])->where("status",1)->first();
                $wmcf->supply_coefficient = null;
                if (!empty($wpc)) {
                    $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                    $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                }
                if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                    $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                }
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->first();
                // if($sip){
                //     $_sa["amount"]=$sip->price;
                // }
                $sip=SupplyCityPrice::where("supply_id",$_sa["id"])->where("city_id",$mirror_orders->city)->where("status",1)->first();
                if($sip){
                    $_sa["amount"]=$sip->price;
                }else{
                    $ss_temp=SupplySub::where("supply_id",$_sa["id"])->where("status",1)->first();
                    $_sa["amount"]=isset($ss_temp)?$ss_temp->usual_price:0;
                }

                $temp_amount=$_sa["amount"]*$wmcf->supply_coefficient;
                $temp_amount=Common::upFow($temp_amount);
                $order_product_item[]=array(
                    "item_id"=>$_sa["id"],
                    "item_name"=>$_sa["name"],
                    "amount"=>$temp_amount,
                    "num"=>$_sa["number"],
                    "type"=>1,
                    "unit"=>$_sa["unit"],
                    "is_adapter"=>$_sa["is_adapter"],
                );
                $count_amount+=$temp_amount*$_sa["number"];
            }
            foreach ($p_item['server'] as $_se) {
                $res=PricingStrategy::getStringStrategy($csi->id,$_se['id'],$sc_user->shop->city_id);
                if($res['code']==0){
                    $temp_amount=$res['price'];
                }else{
                    //判断工时类型为工时类型还是固定价格
                    if($_se['type'] ==1 ){
                        $temp_amount=$whp->price*$wmcf->work_coefficient*$_se["work_hours"];
                    }elseif($_se['type']==2){
                        $temp_amount=$_se["price"];
                    }
                }
                $order_product_item[]=array(
                    "item_id"=>$_se["id"],
                    "item_name"=>$_se["name"],
                    "amount"=>$temp_amount,
                    "num"=>$_se["work_hours"],
                    "type"=>2,
                    "unit"=>"个",
                    "is_adapter"=>1,
                );
                $count_amount+=$temp_amount;
            }
            $package_arr=array(array(
                "product_id"=>$package->id,
                "product_name"=>$package->name,
                "amount"=>$count_amount,
                "item"=>$order_product_item
            ));
            MirrorOrderProduct::saveBaseProducts($package_arr,"",$sc_user->id,$mirror_orders->order_id);
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addProduct", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 15:19:20
     * @Description: 删除产品包（包括标准包、服务、企业包）
     */
    public function delProduct(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                "order_product_id"  => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $order_product=MirrorOrderProduct::find(Input::get("order_product_id"));
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $response=MirrorOrderProduct::deleteProduct($order_product);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "delProduct", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 15:34:10
     * @Description: 删除非标产品包中配件
     */
    public function delProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                "order_product_id"  => 'required',
                'order_product_non_standard_id' => 'required'
            )
            );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $order_product=MirrorOrderProduct::find(Input::get("order_product_id"));
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $opns=MirrorOrderProductNonStandard::where('id',Input::get('order_product_non_standard_id'))->where('status',1)->first();
            if(!$opns){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }

            $opns->status=0;
            $opns->save();

            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            MirrorOrders::calculate(Input::get('order_id'));

            $response=array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "delProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      zwh
     * @DateTime:    2016-10-26 11:07:14
     * @Description: 用于更改基础产品的配件数量
     */
    public function changeBaseProductItemNum(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_base_item_id'=> 'required',
                'number'=> 'required|numeric|min:0|not_in:0',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opbi=MirrorOrderProductBaseItem::find(Input::get("order_product_base_item_id"));
            if(is_null($opbi)||$opbi->order_id!=$orders->order_id||$opbi->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $order_product=MirrorOrderProduct::find($opbi->order_product_package_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $number=Input::get("number");
            $amount=Input::get("amount")*100;
            if($number==$opbi->num&&$amount==$opbi->amount){
            // if($number==$opbi->num){
                $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
                break;
            }
            $response=MirrorOrderProductBaseItem::changeProductItemNum(Input::get('order_id'),$opbi,$number,$amount);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItemNum", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 15:47:02
     * @Description: 用于更换基础产品包的耗材型号
     */
    public function changeBaseProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_base_item_id'=> 'required',
                'new_item_id'=> 'required',
                // 'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opbi=MirrorOrderProductBaseItem::find(Input::get("order_product_base_item_id"));
            if(is_null($opbi)||$opbi->order_id!=$orders->order_id||$opbi->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $order_product=MirrorOrderProduct::find($opbi->order_product_package_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }

            //要更换成的配件类型判断
            // if(Input::get('type')==0){
                $si=SupplyItem::find(Input::get("new_item_id"));
                // if(is_null($si)||$si->status!=1){
                //     $response = Lang::get("scs_error.supply_item_not_exit_41000");
                //     break;
                // }
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',$si->type)->first();
                

                $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->supply_classification_id)->where("status",1)->first();
                $wmcf->supply_coefficient = null;
                if (!empty($wpc)) {
                    $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                    $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                }
                if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                    $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                }
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$sc_user->shop->city_id)->first();
                // if($sip){
                //     $si->amount=$sip->price;
                // }
                // 
                // 
                if($si->type==1||PriceBargaining::ischange($si->id,$orders->shop_id)==1){//如果非标耗材和 标准耗材能编辑价格的
                    $si->amount=Input::get("amount")*100;
                    $max=MaintenanceStorageItem::where('shop_id',$orders->shop_id)->where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',$si->type)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                    if($si->amount < $max){
                        $response = Lang::get("scs_error.data_price_error_61018");
                        break;
                    }
                }else{ 
                    $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                    if($sip){
                        $si->amount=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                        $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
                    $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
                }
                // 

//             }else{
//                 $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
//                 $max=MaintenanceStorageItem::where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',1)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
// //                 if(Input::get('amount')*100 < $max){
// //                     $response = Lang::get("scs_error.data_price_error_61018");
// //                     break;
// //                 }
//                 $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
//                 $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
//             }

            //原有配件类型判断
            // if($opbi->item_type==0){
                $old_si=SupplyItem::find($opbi->item_id);
            // }else{
            //     $old_si=SupplyItemNonStandard::find($opbi->item_id);
            // }
            
            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                $temp_subject=($opbi->temp_subject==0)?$old_si->supply_classification_id:$opbi->temp_subject;//如果
                if($temp_subject!=$si->supply_classification_id){
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }
            //第一次更换耗材记录该耗材的；品类 为了以后更换标准耗材判断 
            if($opbi->temp_subject==0){
                $opbi->temp_subject=$old_si->supply_classification_id;
            }
            $response=MirrorOrderProductBaseItem::changeProductItem(Input::get('order_id'),$opbi,$si,$wmcf,$si->type);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 15:53:31
     * @Description: 用于修改 非标产品的 条目数量
    */
    public function changeNonProductItemNum(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_non_standard_id'=> 'required',
                'number'=> 'required|numeric|min:0|not_in:0',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opns=MirrorOrderProductNonStandard::where('id',Input::get("order_product_non_standard_id"))->where('status',1)->first();
            if(is_null($opns)||$opns->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $order_product=MirrorOrderProduct::find($opns->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $number=Input::get("number");
            $amount=Input::get("amount")*100;
            if($number==$opns->num&&$amount==$opns->amount){
            // if($number==$opns->num){
                $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
                break;
            }
            $response=MirrorOrderProductNonStandard::changeProductItemNum($order_product->order_id,$opns,$number,$amount);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeNonProductItemNum", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 16:16:53
     * @Description: 用于更换非标服务包耗材
     */
    public function changeNonStandardItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                // 'type'   => 'required',
                'order_id' => 'required',
                'order_product_non_standard_id'=> 'required',
                'new_item_id'=> 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opns=MirrorOrderProductNonStandard::where('id',Input::get("item_id"))->where('status',1)->first();
            if(is_null($opns)||$opns->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $order_product=MirrorOrderProduct::find($opns->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
    
            // if(Input::get('type')==0){
            $si=SupplyItem::find(Input::get("new_item_id"));
            // if(is_null($si)||$si->status!=1){//如果 耗材不存在或者 状态不正确
            //     $response = Lang::get("scs_error.supply_item_not_exit_41000");
            //     break;
            // }
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',$si->type)->first();
                $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
                if(is_null($wmcf)){
                    $response = Lang::get("scs_error.work_mcf_error_10002");
                    break;
                }
                // if($opns->type==0){
                $old=SupplyItem::find($opns->item_id);
                // }else{
                //     $old=SupplyItemNonStandard::find($opns->item_id);
                // }
                
                if($old->supply_classification_id!=$si->supply_classification_id){
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
                $spi=DataSupplyProductItem::where("car_model_id",$csi->id)->where("supply_item_id",$si->id)->where("status",1)->first();
                if(is_null($spi)){
                    $response = Lang::get("scs_error.supply_item_car_model_error_41001");
                    break;
                }
    
                // $ss=SupplySeries::find($si->series_id);
                //更换耗材的加价系数
                $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->supply_classification_id)->where("status",1)->first();
                $wmcf->supply_coefficient = null;
                if (!empty($wpc)) {
                    $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                    $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                }
                if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                    $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                }
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$sc_user->shop->city_id)->first();
                // if($sip){
                //     $si->amount=$sip->price;
                // }

                if($si->type==1||PriceBargaining::ischange($si->id,$orders->shop_id)==1){//如果非标耗材和 标准耗材能编辑价格的
                    $si->amount=Input::get("amount")*100;
                    $max=MaintenanceStorageItem::where('shop_id',$orders->shop_id)->where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',$si->type)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                    if($si->amount < $max){
                        $response = Lang::get("scs_error.data_price_error_61018");
                        break;
                    }
                }else{ 
                    $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                    if($sip){
                        $si->amount=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                        $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
                    $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
                }
    
                // $c_amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
                $item_info=SupplyItem::getCacheItemInfo($si->id);
                $opns->item_id=Input::get('new_item_id');
                $opns->name=$item_info->name;
                $opns->amount=$si->amount;
                $opns->type=$si->type;
                $opns->unit=$item_info->unit?$item_info->unit:'';
                $opns->discount=1;
                $opns->save();
    
                //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
                Orders::calculate(Input::get('order_id'));
    
//             }else{
//                 $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
//                 $max=MaintenanceStorageItem::where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',1)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
// //                 if(Input::get('amount')*100 < $max){
// //                     $response = Lang::get("scs_error.data_price_error_61018");
// //                     break;
// //                 }
//                 $sins=SupplyItemNonStandard::find(Input::get("new_item_id"));
//                 $sins->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
//                 if($opns->type==0){
//                     $old=SupplyItem::find($opns->item_id);
//                 }else{
//                     $old=SupplyItemNonStandard::find($opns->item_id);
//                 }
//                 if(is_null($sins)||$sins->status!=1){//如果 耗材不存在或者 状态不正确
//                     $response = Lang::get("scs_error.supply_item_not_exit_41000");
//                     break;
//                 }
//                 if($old->subject!=$sins->subject){
//                     $response = Lang::get("scs_error.supply_subject_error_41002");
//                     break;
//                 }
//                 $moo=MaintenanceOutOrder::where("product_type","non_standard")->where("product_item_id",$opns->id)->whereIn("status",array(0,1,2))->first();
//                 if($moo){//如果存在 备货中或者 等待领料的 出库申请 已经领料的 不允许修改
//                     return Lang::get("scs_error.order_product_item_change_error_40012");
//                 }
    
//                 $opns->name=$sins->series_name." ".$sins->model;
//                 $opns->item_id=Input::get('new_item_id');
//                 $opns->amount=$sins->amount;
//                 $opns->type=Input::get('type');
//                 $opns->unit=$sins->unit;
//                 $opns->discount=1;
//                 $opns->save();
    
//                 //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
//                 MirrorOrders::calculate(Input::get('order_id'));
//             }
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeNonStandardItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 16:02:03
     * @Description: 用于添加 订单的服务
     */
    public function addService(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                "id"=>'required',
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->order_type==1){
                $csi=DataCarStandardInfo::find($orders->car_model_id);
                if(is_null($csi)){
                    $response = Lang::get("scs_error.car_model_is_empty23000");
                    break;
                }
            }
            $service=Service::find(Input::get("id"));
            if(is_null($service)){
                $response = Lang::get("scs_error.package_error_50001");
                break;
            }
            $op=new MirrorOrderProduct();
            $op->status=1;
            $op->amount=0;
            $op->order_id=Input::get('order_id');
            $op->product_type='non_standard';
            $op->product_id=Input::get('id');
            $op->product_name=$service->name;
            $op->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addService", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 16:03:04
     * @Description: 用于添加 配件
     */
    public function addPart(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                // 'type' => 'required',
                "item_id"=>'required',//supply_item的id
                'number'=> 'required|numeric|min:0|not_in:0',
                'order_product_id'=> 'required',
                'amount'=> 'min:0'
            )
            );
        $number=Input::get('number');
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }

            $response=DB::transaction(function () use ($number,$sc_user) {
                $orders=MirrorOrders::where('order_id',Input::get('order_id'))->lockForUpdate()->first();
                if(is_null($orders)){
                    return Lang::get("scs_error.order_notexiet_40000");
                }
                if($orders->shop_id!=$sc_user->shop_id){
                    return Lang::get("scs_error.order_note_shop_error_40003");
                }
                $si=SupplyItem::find(Input::get("item_id"));
                // if(is_null($si)||$si->status!=1){//如果 耗材不存在或者 状态不正确
                //     return Lang::get("scs_error.supply_item_not_exit_41000");
                // }
                $mw=MaintenanceWarehouse::where('item_id',Input::get('item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',$si->type)->first();
                $op=MirrorOrderProduct::where("id",Input::get('order_product_id'))->lockForUpdate()->first();
                if($op->status === 0){
                    return Lang::get("scs_error.order_delete_40017");
                }
                // if(Input::get('type')==0){
                    $csi=new \stdClass();
                    if($orders->order_type==1){
                        $csi=DataCarStandardInfo::find($orders->car_model_id);
                        if(is_null($csi)){//只进行 车型存在 不对车型状态进行祥判断
                            return Lang::get("scs_error.order_car_model_notexit_40002");
                        }
                        $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
                        if(is_null($wmcf)){
                            return Lang::get("scs_error.work_mcf_error_10002");
                        }
                    }
                    
                    // $ss=SupplySeries::find($si->series_id);
                    //更换耗材的加价系数
                    $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->supply_classification_id)->where("status",1)->first();
                    $wmcf->supply_coefficient = null;
                    if (!empty($wpc)) {
                        $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                        $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                    }
                    if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                        $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                    }
                    //更换多城市价格
                    $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$sc_user->shop->city_id)->first();
                    if($sip){
                        $si->amount=$sip->price;
                    }

                    if($si->type==1||PriceBargaining::ischange($si->id,$orders->shop_id)==1){//如果非标耗材和 标准耗材能编辑价格的
                        $si->amount=Input::get("amount")*100;
                        $max=MaintenanceStorageItem::where('shop_id',$orders->shop_id)->where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',$si->type)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                        if($si->amount < $max){
                            $response = Lang::get("scs_error.data_price_error_61018");
                            break;
                        }
                    }else{ 
                        $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                        if($sip){
                            $si->amount=$sip->price;
                        }else{
                            $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                            $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                        }
                        $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
                    }
                    $item_info=SupplyItem::getCacheItemInfo($si->id);
                    $opns=new MirrorOrderProductNonStandard;
                    $opns->order_id=$orders->order_id;
                    $opns->name=$item_info->name;
                    $opns->num=$number;
                    $opns->item_id=$si->id;
                    $opns->amount=$si->amount;
                    $opns->order_product_id=Input::get('order_product_id');
                    $opns->work_station=0;
                    $opns->work_status="prepare";
                    $opns->type=$si->type;
                    $opns->service_type=1;
                    $opns->unit=$item_info->unit?$item_info->unit:'';
                    $opns->save();

                    //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
                    MirrorOrders::calculate(Input::get('order_id'));
//                 }else{
//                     $sins=SupplyItemNonStandard::find(Input::get("item_id"));
//                     if(is_null($sins)||$sins->status!=1){//如果 耗材不存在或者 状态不正确
//                         return Lang::get("scs_error.supply_item_not_exit_41000");
//                     }
//                     $max=MaintenanceStorageItem::where('item_id',Input::get('item_id'))->where('type',1)->where('item_type',1)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
// //                     if(Input::get('amount')*100 < $max){
// //                         return Lang::get("scs_error.data_price_error_61018");
// //                     }
//                     $sins->amount=Input::get("amount")*100;
//                     $opns=new MirrorOrderProductNonStandard;
//                     $opns->order_id=$orders->order_id;
//                     $opns->name=$sins->series_name." ".$sins->model;
//                     $opns->num=$number;
//                     $opns->item_id=$sins->id;
//                     $opns->amount=$sins->amount;
//                     $opns->order_product_id=Input::get('order_product_id');
//                     $opns->work_station=0;
//                     $opns->work_status="prepare";
//                     $opns->type=Input::get('type');
//                     $opns->service_type=1;
//                     $opns->unit=$sins->unit;
//                     $opns->save();

//                     //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
//                     MirrorOrders::calculate(Input::get('order_id'));
//                 }
                return array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
            });
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addPart", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    22016-10-25 16:06:34
     * @Description: 用于添加 工时
     */
    public function addWorkHours(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'order_product_id'=> 'required',
                'amount' => 'required|numeric|min:0'
            )
            );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $op=MirrorOrderProduct::find(Input::get('order_product_id'));
            if(is_null($op)||$op->product_type!='non_standard'){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            $opns=MirrorOrderProductNonStandard::where("order_product_id",$op->id)->where("service_type",2)->where('status',1)->first();
            if($opns){
                $response = Lang::get("scs_error.order_product_work_exit_40032");
                break;
            }
            $opns=new MirrorOrderProductNonStandard;
            $opns->order_id=$orders->order_id;
            $opns->name='工时费';
            $opns->num=0;
            $opns->amount=Input::get('amount')*100;
            $opns->order_product_id=Input::get('order_product_id');
            $opns->work_station=0;
            $opns->work_status="prepare";
            $opns->type=1;
            $opns->unit='个';
            $opns->service_type=2;
            $opns->save();

            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            MirrorOrders::calculate(Input::get('order_id'));
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addService", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    

    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 16:20:41
     * @Description: 用于修改工时费
     */
    public function updateWorkHours(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'order_product_non_standard_id'=> 'required',
                'amount' => 'required|numeric'
            )
            );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opns=MirrorOrderProductNonStandard::where('id',Input::get('order_product_non_standard_id'))->where('status',1)->first();
            $op=OrderProduct::find($opns->order_product_id);

            if($opns->order_id!=Input::get('order_id')){
                $response = Lang::get("scs_error.supply_subject_error_41003");
                break;
            }

            $opns->amount=Input::get('amount')*100;
            $opns->save();
            
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            MirrorOrders::calculate(Input::get('order_id'));
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "updateWorkHours", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-10-27 11:00:06
     * @Description: 非标 配件工时打折
     */
    public function nonStandardDiscount(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_non_standard_id'=> 'required',
                'discount'=> 'required|numeric|max:1',
            )
            );
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }

            $discount=Input::get("discount");
            $response=DB::transaction(function () use ($discount,$sc_user) {
                $response = array('code' => 0, 'msg' => 'ok');
                $orders=MirrorOrders::where('order_id',Input::get('order_id'))->lockForUpdate()->first();
                if(is_null($orders)){
                    return $response = Lang::get("scs_error.order_notexiet_40000");
                }
                if($orders->shop_id!=$sc_user->shop_id){
                    return $response = Lang::get("scs_error.order_note_shop_error_40003");
                }
                $opns=MirrorOrderProductNonStandard::where('id',Input::get("order_product_non_standard_id"))->where('status',1)->lockForUpdate()->first();
                if(is_null($opns)||$opns->order_id!=$orders->order_id){
                    return $response = Lang::get("scs_error.order_product_item_notexit_40011");
                }
                $order_product=MirrorOrderProduct::where('id',$opns->order_product_id)->lockForUpdate()->first();
                if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                    return $response = Lang::get("scs_error.order_product_notexit_40006");
                }
                if($order_product->status!=1){
                    return $response = Lang::get("scs_error.order_product_status_error_40009");
                }

                // 先将折扣信息保存
                $opns->discount=$discount;
                $opns->save();
                
                //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
                MirrorOrders::calculate(Input::get('order_id'));
                $response['result']=array('price'=>$opns->amount*$discount);
                return $response;
            });
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "non_standard_discount", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 16:36:39
     * @Description: 标准 配件工时打折
     */
    public function discount(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_base_item_id'=> 'required',
                'discount'=> 'required|numeric|max:1',
            )
            );

        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $discount=Input::get("discount");
            $response=DB::transaction(function () use ($discount,$sc_user) {
                $response = array('code' => 0, 'msg' => 'ok');
                $orders=MirrorOrders::where('order_id',Input::get('order_id'))->lockForUpdate()->first();
                if(is_null($orders)){
                    return $response = Lang::get("scs_error.order_notexiet_40000");
                }
                if($orders->shop_id!=$sc_user->shop_id){
                    return $response = Lang::get("scs_error.order_note_shop_error_40003");
                }
                $opbi=MirrorOrderProductBaseItem::where('id',Input::get("order_product_base_item_id"))->lockForUpdate()->first();
                if(is_null($opbi)||$opbi->order_id!=$orders->order_id){
                    return $response = Lang::get("scs_error.order_product_item_notexit_40011");
                }
                $order_product=MirrorOrderProduct::where('id',$opbi->order_product_package_id)->lockForUpdate()->first();
                if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                    return $response = Lang::get("scs_error.order_product_notexit_40006");
                }
                if($order_product->status!=1){
                    return $response = Lang::get("scs_error.order_product_status_error_40009");
                }
                
                // 先将折扣信息保存
                $opbi->discount=$discount;
                $opbi->save();
                
                //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
                MirrorOrders::calculate(Input::get('order_id'));
                $response['result']=array('price'=>$opbi->amount*$discount);
                return $response;
            });
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "discount", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-27 11:31:58
     * @Description: 批量配件工时打折
     */
    public function discounts(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'discount'=> 'required|numeric|max:1',
            )
        );
    
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $discount=Input::get("discount");
            $response=DB::transaction(function () use ($discount,$sc_user) {
                $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
                $orders=MirrorOrders::where('order_id',Input::get('order_id'))->lockForUpdate()->first();
                if(is_null($orders)){
                    return $response = Lang::get("scs_error.order_notexiet_40000");
                }
                if($orders->shop_id!=$sc_user->shop_id){
                    return $response = Lang::get("scs_error.order_note_shop_error_40003");
                }
                $order_product=MirrorOrderProduct::where('order_id',Input::get('order_id'))->where('status',1)->lockForUpdate()->get();
                foreach($order_product as $op){
                    if($op->product_type=='base'){
                        $opbi=MirrorOrderProductBaseItem::where('order_product_package_id',$op->id)->lockForUpdate()->get();
                        foreach($opbi as $_opbi){
                            $_opbi->discount=$discount;
                            $_opbi->save();
                        }
                    }
                    if($op->product_type=='non_standard'){
                        $opns=MirrorOrderProductNonStandard::where('order_product_id',$op->id)->where('status',1)->lockForUpdate()->get();
                        foreach($opns as $_opns){
                            $_opns->discount=$discount;
                            $_opns->save();
                        }
                    }
                    
                }
                $orders->discount_rate=$discount;
                $orders->save();
                
                //订单应付金额及优惠信息将重新计算，废弃原有累计加减算法
                MirrorOrders::calculate(Input::get('order_id'));
                return $response;
            });
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "discounts", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-26 10:28:14
     * @Description: 用于添加 订单企业包
     */
    public function addSetmeal(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'id'       => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.car_model_is_empty23000");
                break;
            }
            $es = EnterpriseSetmeal::find(Input::get('id'));
            $pids = explode(',',$es->package_ids);
            $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            if(is_null($whp)){
                $response = Lang::get("scs_error.work_hour_error_10001");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $response=MirrorOrderProduct::implement($pids,$sc_user,$orders,$wmcf,$whp,$es,$csi);
        }while(false);
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-4-11 14:04:47
     * @Description: 用于添加 订单添加活动包
     */
    public function addProductActivity(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'id'       => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.car_model_is_empty23000");
                break;
            }
            $pa = ProductActivity::find(Input::get('id'));
            $pap=ProductActivityPackage::where('product_activity_id',Input::get('id'))->select('package_id')->get()->toArray();
            $pids= array_column($pap,'package_id');
            $whp=WorkHoursPrice::where("city_id",$sc_user->shop->city_id)->first();
            if(is_null($whp)){
                $response = Lang::get("scs_error.work_hour_error_10001");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $response=MirrorOrderProduct::addProductActivity($pids,$sc_user,$orders,$wmcf,$whp,$pa,$csi);
        }while(false);
        return json_encode($response);
    }
    
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-26 10:17:23
     * @Description: 用于更改企业套产品数量
     */
    public function changeEsItemNum(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_esitem_id'=> 'required',
                'number'=> 'required|numeric|min:0|not_in:0',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opesi=MirrorOrderProductEsItem::find(Input::get("order_product_esitem_id"));
            if(is_null($opesi)||$opesi->order_id!=$orders->order_id||$opesi->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opep=MirrorOrderProductEsPackage::find($opesi->order_product_package_id);
            $order_product=MirrorOrderProduct::find($opep->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $number=Input::get("number");
            $amount=Input::get("amount")*100;
            if($number==$opesi->num&&$amount==$opesi->amount){
            // if($number==$opesi->num){
                $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
                break;
            }
            $response=MirrorOrderProductEsItem::changeProductItemNum($order_product,$opesi,$number,$amount);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeEsItemNum", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-26 10:17:23
     * @Description: 用于更改活动包产品数量
     */
    public function changeAcItemNum(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_acitem_id'=> 'required',
                'number'=> 'required|numeric|min:0|not_in:0',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opaci=MirrorOrderProductAcItem::find(Input::get("order_product_acitem_id"));
            if(is_null($opaci)||$opaci->order_id!=$orders->order_id||$opaci->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opacp=MirrorOrderProductAcPackage::find($opaci->order_product_package_id);
            $order_product=MirrorOrderProduct::find($opacp->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $number=Input::get("number");
            $amount=Input::get("amount")*100;
            if($number==$opaci->num&&$amount==$opaci->amount){
                // if($number==$opesi->num){
                $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
                break;
            }
            $response=MirrorOrderProductAcItem::changeProductItemNum($order_product,$opaci,$number,$amount);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeAcItemNum", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-26 10:16:25
     * @Description: 用于虚拟单更换企业包配件
     */
    public function changeEsProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_esitem_id'=> 'required',//订单耗材的id
                'new_item_id'=> 'required',
                // 'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opei=MirrorOrderProductEsItem::find(Input::get("order_product_esitem_id"));
            if(is_null($opei)||$opei->order_id!=$orders->order_id||$opei->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opep=MirrorOrderProductEsPackage::find($opei->order_product_package_id);
            $order_product=MirrorOrderProduct::find($opep->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }


            $si=SupplyItem::find(Input::get("new_item_id"));
            // if(is_null($si)||$si->status!=1){
            //     $response = Lang::get("scs_error.supply_item_not_exit_41000");
            //     break;
            // }
            $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->supply_classification_id)->where("status",1)->first();
            $wmcf->supply_coefficient = null;
            if (!empty($wpc)) {
                $wpc_city=WorkPartsCoefficientCity::where('city_id',$orders->city)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
            }
            if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
            }
            //要更换成的配件类型判断
            // if($si->type==0){
                // $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',$si->type)->first();
                
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$orders->city)->first();
                // if($sip){
                //     $si->amount=$sip->price;
                // }
                if($si->type==1||PriceBargaining::ischange($si->id,$orders->shop_id)==1){//如果非标耗材和 标准耗材能编辑价格的
                    $si->amount=Input::get("amount")*100;
                    $max=MaintenanceStorageItem::where('shop_id',$orders->shop_id)->where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',$si->type)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                    if($si->amount < $max){
                        $response = Lang::get("scs_error.data_price_error_61018");
                        break;
                    }
                }else{ 
                    $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                    if($sip){
                        $si->amount=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                        $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
                    $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
                }
            // }else{
            //     $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
            //     $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            // }
    
            //原有配件类型判断
            // if($opei->item_type==0){
            $old_si=SupplyItem::find($opei->item_id);
            // }else{
            //     $old_si=SupplyItemNonStandard::find($opei->item_id);
            // }
           
            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                if($old_si->supply_classification_id!=$si->supply_classification_id){
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }
            //第一次更换耗材记录该耗材的；品类 为了以后更换标准耗材判断 
            if($opei->temp_subject==0){
                $opei->temp_subject=$old_si->supply_classification_id;
            }
            $response=MirrorOrderProductEsItem::changeProductItem($order_product,$opei,$si,$wmcf,$si->type,$csi);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeEsProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-4-15 10:48:51
     * @Description: 用于虚拟单更换活动包配件
     */
    public function changeAcProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_acitem_id'=> 'required',//订单耗材的id
                'new_item_id'=> 'required',
                // 'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opaci=MirrorOrderProductAcItem::find(Input::get("order_product_acitem_id"));
            if(is_null($opaci)||$opaci->order_id!=$orders->order_id||$opaci->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opacp=MirrorOrderProductAcPackage::find($opaci->order_product_package_id);
            $order_product=MirrorOrderProduct::find($opacp->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
            $si=SupplyItem::find(Input::get("new_item_id"));
            // if(is_null($si)||$si->status!=1){
            //     $response = Lang::get("scs_error.supply_item_not_exit_41000");
            //     break;
            // }
            $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->supply_classification_id)->where("status",1)->first();
            $wmcf->supply_coefficient = null;
            if (!empty($wpc)) {
                $wpc_city=WorkPartsCoefficientCity::where('city_id',$orders->city)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
            }
            if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
            }

            //要更换成的配件类型判断
            // if(Input::get('type')==0){
                // $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',0)->first();
                // $si=SupplyItem::find(Input::get("new_item_id"));
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$orders->city)->first();
                // if($sip){
                //     $si->amount=$sip->price;
                // }
                if($si->type==1||PriceBargaining::ischange($si->id,$orders->shop_id)==1){//如果非标耗材和 标准耗材能编辑价格的
                    $si->amount=Input::get("amount")*100;
                    $max=MaintenanceStorageItem::where('shop_id',$orders->shop_id)->where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',$si->type)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                    if($si->amount < $max){
                        $response = Lang::get("scs_error.data_price_error_61018");
                        break;
                    }
                }else{ 
                    $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                    if($sip){
                        $si->amount=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                        $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
                    $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
                }
            // }else{
            //     $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
            //     $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
            //     $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            // }
    
            //原有配件类型判断
            // if($opaci->item_type==0){
                $old_si=SupplyItem::find($opaci->item_id);
            // }else{
            //     $old_si=SupplyItemNonStandard::find($opaci->item_id);
            // }
            
            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                if($old_si->supply_classification_id!=$si->supply_classification_id){
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }
            //第一次更换耗材记录该耗材的；品类 为了以后更换标准耗材判断
            if($opaci->temp_subject==0){
                $opaci->temp_subject=$old_si->supply_classification_id;
            }
            $response=MirrorOrderProductAcItem::changeProductItem($order_product,$opaci,$si,$wmcf,$si->type,$csi);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeEsProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-26 17:31:30
     * @Description: 根据订单查询 匹配的耗材 
     */
    public function supply_search(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $car_retrieval_code=Input::get("car_retrieval_code");
            $type=Input::get("type");
            $search_retrieval_code=Input::get("retrieval_code");

            $car_str=Input::get("car_str");
            if($type||$search_retrieval_code||$car_str){
                // $non_ids=SupplyItemNonStandard::retrievalCode($sc_user->shop_id,$car_retrieval_code,$type,$search_retrieval_code,$car_str);//获得非标的耗材id范围
                $si_ids=SupplyItem::retrievalCode($car_retrieval_code,$type,$search_retrieval_code,$car_str);//获得标准的耗材id范围
            }
            // else{
            //     $response = Lang::get("scs_error.supply_item_searce_args_not_exit_41010");
            //     break;
            // }
            if(isset($si_ids)&&sizeof($si_ids)>5000){
                $response = Lang::get("scs_error.supply_item_searce_args_not_exit_41011");
                break;
            }

            $si_sb = DB::table('supply_item')
            ->leftJoin('anhui_data.supply_brand',"supply_brand.id","=","supply_item.brand_id")
            ->distinct('supply_brand.brand_name')->select('supply_brand.brand_name');   
            if(isset($si_ids)){
                $si_sb =$si_sb->whereIn("supply_item.id",$si_ids);
            }
            $si_sb = $si_sb->get();
            // $snsi_sb = DB::table('supply_item_non_standard')->distinct('brand_name')->select('brand_name');
            // if(isset($non_ids)){
            //     $snsi_sb =$snsi_sb->whereIn("id",$non_ids);
            // }
            // $snsi_sb = $snsi_sb->get();
    
            //标准配件列表
            $sql="select si.id,si.type,mw.number,1 as  recommend,spi.isdefualt from supply_item as si
                Left Join anhui_data.supply_product_item spi on spi.supply_item_id=si.id
                Left Join (select item_type,item_id,shop_id,number from maintenance_warehouse where shop_id=".$sc_user->shop_id.") mw on mw.item_id=si.id
                where spi.car_model_id=".$orders->car_model_id."  and spi.status=1 and si.status=1 ";
            $sql2="select si.id,si.type,mw.number,0 as  recommend,0 as isdefualt from supply_item as si
                Left Join (select item_type,item_id,shop_id,number from maintenance_warehouse where shop_id=".$sc_user->shop_id.") mw on mw.item_id=si.id
                where si.status=1  ";
    
            if(Input::get('subject')){
                $sql.=" and supply_classification_id = ".Input::get('subject');
                $sql2.=" and supply_classification_id = ".Input::get('subject');
            }
            if(Input::get('brand_name')){
                $sql.=" and si.brand_name like '%".Input::get('brand_name')."%'";
                $sql2.=" and si.brand_name like '%".Input::get('brand_name')."%'";
            }
            if(isset($si_ids)){
                $si_ids[]=0;
                $sql.=" and si.id in (".implode(",",$si_ids).")"; 
            }
            $arr=DB::select($sql." limit 20");//查询 匹配的标准配件
            //标准配件分为推荐和所有标准配件
            $pipei_id=array();
    
            //所有配件中取出推荐配件（去重）
            foreach($arr as $key=>$a){
                $pipei_id[]=$a->id;
            }
            if(isset($si_ids)){
                $temp_ids=array_diff($si_ids, $pipei_id);
                $temp_ids[]=0;
                $sql2.=" and si.id in (".implode(",",$temp_ids).")";
            }else{
                if(sizeof($pipei_id) >= 1){
                    $sql2.=" and si.id not in(".implode(",",$pipei_id).")";
                }
            }
            $arr2=DB::select($sql2." limit 20");//查询 所有的标准配件
            $temp1=array_merge($arr,$arr2);
            //非标产品列表
            // $sql1="select id,number from supply_item_non_standard as sion left Join (select item_type,item_id,shop_id,number from maintenance_warehouse where item_type=1 and shop_id=".$sc_user->shop_id.") mw on mw.item_id=sion.id where 1=1  ";
            // if(isset($non_ids)){
            //     $non_ids[]=0;
            //     $sql1.=" and sion.id in (".implode(",",$non_ids).")";
            // }
            // $arr1=DB::select($sql1." limit 20");
            $csi=DataCarModel::find($orders->car_model_id); 
            // $tuijian=SupplyRecommend::getRecommendByCarModel($csi);
            
            $finally=$recommend=$isdefualt=array();
            foreach($temp1 as $t){
                $si=SupplyItem::getCacheItemInfo($t->id);
                $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$orders->city)->first();
                if($sip){
                    $si->amount=$sip->price;
                }
                
                $supply_coefficient = null;
                if($csi){
                    $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->supply_classification_id)->where("status",1)->first();                
                    if (!empty($wpc)) {
                        $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                        $supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                    }
                }                
                if ($supply_coefficient == null){ //如果是null，说明没有赋值
                    $supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                }

                $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                if($sip){
                    $si->amount=$sip->price;
                }else{
                    $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                    $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                }
                // if(in_array($si->id, $tuijian)){
                //     $t->isdefualt=1;
                // }
                $si->amount=$si->amount*$supply_coefficient;
                // $t->subject=$si->subject;
                // $t->code=$si->code;
                // $t->s_name=$si->subject_name;
                // $t->model=$si->model;
                // $t->amount=$si->amount;
                // $t->brand_name=$si->brand_name;
                // $t->se_name=$si->series_name;
                // $t->scf_id=$si->st_classifi_id;
                // $t->scf_name=$si->scf_name;
                // $t->specification=$si->specification;
                // $t->car_model_comments=$si->car_model_comments;
                $item_array=array(
                    "id"=>$t->id,
                    "item_type"=>$t->type,
                    "number"=>$t->number,
                    "recommend"=>$t->recommend,
                    "isdefualt"=>$t->isdefualt,
                    "subject"=>$si->subject,
                    "code"=>$si->code,
                    "s_name"=>$si->subject_name,
                    "model"=>$si->model,
                    "amount"=>$si->amount,
                    "brand_name"=>$si->brand_name,
                    "se_name"=>$si->series_name,
                    "scf_id"=>$si->st_classifi_id,
                    "scf_name"=>$si->scf_name,
                    "specification"=>$si->specification,
                    "car_model_comments"=>$si->car_model_comments,
                    // "ischange"=>PriceBargaining::ischange($t->id,$orders->shop_id)
                    "item_name"=>$si->name,
                );
                $finally[]=$item_array;
                $recommend[]=$t->recommend;
                $isdefualt[]=$t->isdefualt;
            }
            // foreach($arr1 as $a){
            //     $sins=SupplyItemNonStandard::getCacheItemInfo($a->id);
            //     $a->code=$sins->code;
            //     $a->car_model=$sins->car_model;
            //     $a->subject=$sins->subject;
            //     $a->s_name=$sins->subject_name;
            //     $a->model=$sins->model;
            //     $a->amount=$sins->amount;
            //     $a->brand_name=$sins->brand_name;
            //     $a->series_name=$sins->series_name;
            //     $a->scf_id=$sins->st_classifi_id;
            //     $a->scf_name=$sins->scf_name;
            //     $a->specification=$sins->specification;
            //     $a->old_code=$sins->old_code;
            //     $a->retrieval_code=$sins->retrieval_code;
            // }
            array_multisort($recommend, SORT_DESC, $isdefualt, SORT_DESC, $finally);
            $subjects=SupplyClassification::whereIn("id",Config::get("config.maintenance_classification"))->select('id','name')->get();
            $result=array('si'=>$finally,'snsi'=>array(),'subjects'=>$subjects,'si_sb'=>$si_sb,'snsi_sb'=>array());
            $response['result']=$result;
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "supply_search", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-27 15:38:00
     * @Description: 修改虚拟订单金额
     */
    public function update_order_amount(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'discount_amount' => 'required',
                'balance_amount' => 'required',
                'payable_amount' => 'required',
                'credit_amount' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mirror_orders=MirrorOrders::find(Input::get('order_id'));
            $mirror_orders->discount_amount=Input::get('discount_amount')*100;
            $mirror_orders->balance_amount=Input::get('balance_amount')*100;
            $mirror_orders->payable_amount=Input::get('payable_amount')*100;
            $mirror_orders->credit_amount=Input::get('credit_amount')*100;
            $mirror_orders->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "update_order_amount", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-11-7 10:41:48
     * @Description: 用于无适配耗材更换基础产品包的耗材
     */
    public function changeNoAdapterBaseProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_base_item_id'=> 'required',//order_product_base_item的id
                'new_item_id'=> 'required',
                // 'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opbi=MirrorOrderProductBaseItem::find(Input::get("order_product_base_item_id"));
            if(is_null($opbi)||$opbi->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $order_product=MirrorOrderProduct::find($opbi->order_product_package_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
    
            //要更换成的配件类型判断
            // if(Input::get('type')==0){
                // $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',0)->first();
                // if(!$mw || $opbi->num>$mw->number){
                //     $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                //     break;
                // }
                // $si=SupplyItem::find(Input::get("new_item_id"));

                $si=SupplyItem::find(Input::get("new_item_id"));
                // if(is_null($si)||$si->status!=1){
                //     $response = Lang::get("scs_error.supply_item_not_exit_41000");
                //     break;
                // }

                $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->supply_classification_id)->where("status",1)->first();
                $wmcf->supply_coefficient = null;
                if (!empty($wpc)) {
                    $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                    $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                }
                if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                    $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                }
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$sc_user->shop->city_id)->first();
                // if($sip){
                //     $si->amount=$sip->price;
                // }
                if($si->type==1||PriceBargaining::ischange($si->id,$orders->shop_id)==1){//如果非标耗材和 标准耗材能编辑价格的
                    $si->amount=Input::get("amount")*100;
                    // $max=MaintenanceStorageItem::where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',$si->type)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                    // if($si->amount < $max){
                    //     $response = Lang::get("scs_error.data_price_error_61018");
                    //     break;
                    // }
                }else{ 
                    $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                    if($sip){
                        $si->amount=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                        $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
                    $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
                }
    
            // }else{
                // $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
                // if(!$mw || $opbi->num>$mw->number){
                //     $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                //     break;
                // }
                // $max=MaintenanceStorageItem::where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',1)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                // if(Input::get('amount')*100 < $max){
                //     $response = Lang::get("scs_error.data_price_error_61018");
                //     break;
                // }
            //    $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
            //    $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            // }
            // if(is_null($si)||$si->status!=1){
            //     $response = Lang::get("scs_error.supply_item_not_exit_41000");
            //     break;
            // }
            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                if($opbi->item_id!=$si->supply_classification_id){
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }
             //第一次更换耗材记录该耗材的；品类 为了以后更换标准耗材判断 
            // if($opbi->temp_subject==0){
            $opbi->temp_subject=$opbi->item_id;
            // }
            $response=MirrorOrderProductBaseItem::changeProductItem(Input::get('order_id'),$opbi,$si,$wmcf,$si->type);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-11-7 11:01:59
     * @Description: 用于无适配耗材更换基础产品包的耗材
     */
    public function changeNoAdapterEsProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_esitem_id'=> 'required',//订单耗材的id
                'new_item_id'=> 'required',
                // 'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opei=MirrorOrderProductEsItem::find(Input::get("order_product_esitem_id"));
            if(is_null($opei)||$opei->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opep=MirrorOrderProductEsPackage::find($opei->order_product_package_id);
            $order_product=MirrorOrderProduct::find($opep->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
    
            //要更换成的配件类型判断
            // if(Input::get('type')==0){
                // $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',0)->first();
                // if(!$mw || $opei->num>$mw->number){
                //     $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                //     break;
                // }
                $si=SupplyItem::find(Input::get("new_item_id"));
                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$orders->city)->first();
                // if($sip){
                //     $si->amount=$sip->price;
                // }

               if($si->type==1||PriceBargaining::ischange($si->id,$orders->shop_id)==1){//如果非标耗材和 标准耗材能编辑价格的
                    $si->amount=Input::get("amount")*100;
                    // $max=MaintenanceStorageItem::where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',$si->type)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                    // if($si->amount < $max){
                    //     $response = Lang::get("scs_error.data_price_error_61018");
                    //     break;
                    // }
                }else{ 
                    $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                    if($sip){
                        $si->amount=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                        $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
                    $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
                } 
            // }else{
            //     $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
            //     if(!$mw || $opei->num>$mw->number){
            //         $response = Lang::get("scs_error.supply_item_low_stocks_40005");
            //         break;
            //     }
            //     $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
            //     $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            // }
    
            // if(is_null($si)||$si->status!=1){
            //     $response = Lang::get("scs_error.supply_item_not_exit_41000");
            //     break;
            // }
            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                if($opei->item_id!=$si->supply_classification_id){//在添加无适配耗材时耗材id存入的是品类id
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }
             //第一次更换耗材记录该耗材的；品类 为了以后更换标准耗材判断 
            // if($opbi->temp_subject==0){
            $opei->temp_subject=$opei->item_id;
            // }
            $response=MirrorOrderProductEsItem::changeProductItem($order_product,$opei,$si,$wmcf,$si->type,$csi);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-4-15 10:52:11
     * @Description: 用于无适配耗材更换活动包的耗材
     */
    public function changeNoAdapterAcProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'order_product_acitem_id'=> 'required',//订单耗材的id
                'new_item_id'=> 'required',
                'type' => 'required'//0为标准耗材1为非标
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=MirrorOrders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $opaci=MirrorOrderProductAcItem::find(Input::get("order_product_acitem_id"));
            if(is_null($opaci)||$opaci->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $opacp=MirrorOrderProductAcPackage::find($opaci->order_product_package_id);
            $order_product=MirrorOrderProduct::find($opacp->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(is_null($csi)){
                $response = Lang::get("scs_error.order_car_model_notexit_40002");
                break;
            }
            $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            if(is_null($wmcf)){
                $response = Lang::get("scs_error.work_mcf_error_10002");
                break;
            }
    
            //要更换成的配件类型判断
            // if(Input::get('type')==0){
                // $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',0)->first();
                // if(!$mw || $opaci->num>$mw->number){
                //     $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                //     break;
                // }
                $si=SupplyItem::find(Input::get("new_item_id"));
                //更换多城市价格
                if($si->type==1||PriceBargaining::ischange($si->id,$orders->shop_id)==1){//如果非标耗材和 标准耗材能编辑价格的
                    $si->amount=Input::get("amount")*100;
                    // $max=MaintenanceStorageItem::where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',$si->type)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                    // if($si->amount < $max){
                    //     $response = Lang::get("scs_error.data_price_error_61018");
                    //     break;
                    // }
                }else{ 
                    $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                    if($sip){
                        $si->amount=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                        $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
                    $si->amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
                } 
            // }else{
            //     $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
            //     if(!$mw || $opaci->num>$mw->number){
            //         $response = Lang::get("scs_error.supply_item_low_stocks_40005");
            //         break;
            //     }
            //     $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
            //     $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            // }
    
            // if(is_null($si)||$si->status!=1){
            //     $response = Lang::get("scs_error.supply_item_not_exit_41000");
            //     break;
            // }
            //如果更换非标不进行 耗材品类判断
            if($si->type==0){
                if($opaci->item_id!=$si->supply_classification_id){//在添加无适配耗材时耗材id存入的是品类id
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
            }
            //第一次更换耗材记录该耗材的；品类 为了以后更换标准耗材判断
            // if($opbi->temp_subject==0){
            $opaci->temp_subject=$opaci->item_id;
            // }
            $response=MirrorOrderProductAcItem::changeProductItem($order_product,$opaci,$si,$wmcf,$si->type,$csi);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeNoAdapterAcProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}